<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="系统名称" prop="systemName">
         <el-select v-model="queryParams.systemName" placeholder="请选择系统名称" clearable size="small">
          <el-option label="Portal" value="Portal" />
          <el-option label="EC" value="EC" />
          <el-option label="HR" value="HR" />
          <el-option label="OA" value="OA" />
        </el-select>
      </el-form-item>
      <el-form-item label="办理类型" prop="pendType">
        <el-select v-model="queryParams.pendType" placeholder="请选择办理类型" clearable size="small">
          <el-option label="待办" value="1" />
          <el-option label="待阅" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="发起人工号" prop="promoterNo">
        <el-input
          v-model="queryParams.promoterNo"
          placeholder="请输入发起人工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发起人" prop="promoter">
        <el-input
          v-model="queryParams.promoter"
          placeholder="请输入流程发起人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="流程发起部门" prop="promoterOrg">
        <el-input
          v-model="queryParams.promoterOrg"
          placeholder="请输入流程发起部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="待办/待阅当前节点名称" prop="node">
        <el-input
          v-model="queryParams.node"
          placeholder="请输入待办/待阅当前节点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="审批人工号" prop="receiverUserCode">
        <el-input
          v-model="queryParams.receiverUserCode"
          placeholder="请输入审批人工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批人" prop="receiverUser">
        <el-input
          v-model="queryParams.receiverUser"
          placeholder="请输入审批人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程名称" prop="procedureName">
        <el-input
          v-model="queryParams.procedureName"
          placeholder="请输入流程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="流程编码" prop="procedureNo">
        <el-input
          v-model="queryParams.procedureNo"
          placeholder="请输入流程编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="消息标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入消息标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="流程ID" prop="sequenceNo">
        <el-input
          v-model="queryParams.sequenceNo"
          placeholder="请输入流程ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="initiationTime">
        <el-date-picker
          v-model="oneDateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker> -->
        <!-- <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.initiationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择流程创建时间">
        </el-date-picker> -->
      <!-- </el-form-item> --> 
      <el-form-item label="接收时间" prop="receiverTime">
          <el-date-picker
          v-model="twoDateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker> 
      </el-form-item> 
        <el-form-item label="流程地址" prop="pcUrl">
        <el-select v-model="queryParams.pcUrl" placeholder="流程地址是否为空" clearable size="small">
          <el-option label="空" value="空" />
          <el-option label="非空" value="非空" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option label="处理完成" value="1" />
          <el-option label="未处理" value="2" />
          <el-option label="已撤回" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:pend:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:pend:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:pend:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="syncWorkItemOa"
          v-hasPermi="['system:pend:edit']"
        >同步OA待办待阅</el-button>
      </el-col>
       <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="syncWorkItemHr"
          v-hasPermi="['system:pend:edit']"
        >同步HR待办待阅</el-button>
      </el-col>
       <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="syncWorkItemBpm"
          v-hasPermi="['system:pend:edit']"
        >同步Bpm待办待阅</el-button>
      </el-col>
      <el-col :span="1.5" style="color:red;" v-text="'点击HR/OA/BPM待办同步按钮时，需先运行相对的DI文件'"/>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
      <div :span="1.5" style="color:red; font-size: 5px; " v-text="syncOaMessage"/>
      <div :span="1.5" style="color:red; font-size: 5px; " v-text="syncHrMessage"/>
      <div :span="1.5" style="color:red; font-size: 5px; " v-text="syncBpmMessage"/>

    <el-table v-loading="loading" :data="pendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="系统名称" width="80" align="center" prop="systemName" />
      <el-table-column label="流程名称" width="250" align="center" prop="procedureName" />
      <el-table-column label="消息标题" width="300" align="center" prop="title" />
      <el-table-column label="发起人" width="100" align="center" prop="promoter" />
      <!-- <el-table-column label="发起人工号" align="center" prop="promoterNo" /> -->
      <el-table-column label="审批人" width="100" align="center" prop="receiverUser" />
      <!-- <el-table-column label="审批人工号" align="center" prop="receiverUserCode" /> -->
      <el-table-column label="办理类型" width="100" align="center" prop="pendType" >
         <template slot-scope="scope">
         <span>{{ pendTypeFormat(scope.row.pendType) }}</span>
         </template>
      </el-table-column>
      
     
      <!-- <el-table-column label="流程发起部门" align="center" prop="promoterOrg" /> -->
      <!-- <el-table-column label="待办/待阅当前节点名称" align="center" prop="node" /> -->
      
   
      <!-- <el-table-column label="流程编码" align="center" prop="procedureNo" /> -->
    
      <!-- <el-table-column label="流程ID" align="center" prop="sequenceNo" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="initiationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.initiationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="接收时间"  align="center" prop="receiverTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiverTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" width="100" align="center" prop="status" >
        <template slot-scope="scope">
          <span>{{ pendStatusFormat(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pend:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:pend:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改待办待阅信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="系统名称" prop="systemName">
          <el-input v-model="form.systemName" placeholder="请输入系统名称" />
        </el-form-item>
        <el-form-item label="办理类型" prop="pendType">
          <el-select v-model="form.pendType" placeholder="请选择办理类型">
            <el-option label="待办" value="1" />
            <el-option label="待阅" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="发起人工号" prop="promoterNo">
          <el-input v-model="form.promoterNo" placeholder="请输入发起人工号" />
        </el-form-item>
        <el-form-item label="发起人" prop="promoter">
          <el-input v-model="form.promoter" placeholder="请输入发起人" />
        </el-form-item>
        <el-form-item label="发起部门" prop="promoterOrg">
          <el-input v-model="form.promoterOrg" placeholder="请输入发起部门" />
        </el-form-item>
        <el-form-item label="节点名称" prop="node">
          <el-input v-model="form.node" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="办理人工号" prop="receiverUserCode">
          <el-input v-model="form.receiverUserCode" placeholder="请输入办理人工号" />
        </el-form-item>
        <el-form-item label="流程名称" prop="procedureName">
          <el-input type="textarea" v-model="form.procedureName" placeholder="请输入流程名称" />
        </el-form-item>
        <!-- <el-form-item label="流程编码" prop="procedureNo">
          <el-input v-model="form.procedureNo" placeholder="请输入流程编码" />
        </el-form-item> -->
        <el-form-item label="消息标题" prop="title">
          <el-input type="textarea" v-model="form.title" placeholder="请输入消息标题" />
        </el-form-item>
        <!-- <el-form-item label="流程ID" prop="sequenceNo">
          <el-input v-model="form.sequenceNo" placeholder="请输入流程ID" />
        </el-form-item> -->
        <!-- <el-form-item label="创建时间" prop="initiationTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.initiationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择流程创建时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="接收时间" prop="receiverTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.receiverTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择流程待办/待阅接收时间点">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="流程地址" prop="pcUrl">
          <el-input  type="textarea"  autosize="true" v-model="form.pcUrl" placeholder="请输入流程地址" />
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select v-model="form.status" placeholder="请选择处理状态" clearable size="small">
          <el-option label="处理完成" value="1" />
          <el-option label="未处理" value="2" />
          <el-option label="已撤回" value="3" />
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPend, getPend, delPend, addPend, updatePend, exportPend,syncWorkItemHr,syncWorkItemOa,syncWorkItemBpm } from "@/api/system/pend";
import { listLog, getLog, delLog, addLog, updateLog, exportLog } from "@/api/system/syncLog";

export default {
  name: "Pend",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待办待阅信息表格数据
      pendList: [],
      oneDateRange:[],
      twoDateRange:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      syncOaMessage :"OA查询中，请等待",
      syncHrMessage :"HR查询中，请等待",
      syncBpmMessage :"BPM查询中，请等待",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemName: null,
        pendType: null,
        promoterNo: null,
        promoter: null,
        promoterOrg: null,
        node: null,
        receiverUserCode: null,
        receiverUser:null,
        procedureName: null,
        procedureNo: null,
        title: null,
        sequenceNo: null,
        initiationTime: null,
        receiverTime: null,
        pcUrl: null,
        status: null,
        dateType:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        systemName: [
          { required: true, message: "系统名称不能为空", trigger: "blur" }
        ],
        promoterNo: [
          { required: true, message: "流程发起人工号不能为空", trigger: "blur" }
        ],
        promoter: [
          { required: true, message: "流程发起人名字不能为空", trigger: "blur" }
        ],
        promoterOrg: [
          { required: true, message: "流程发起部门不能为空", trigger: "blur" }
        ],
        node: [
          { required: true, message: "待办/待阅当前节点名称不能为空", trigger: "blur" }
        ],
        receiverUserCode: [
          { required: true, message: "待办/待阅接收人不能为空", trigger: "blur" }
        ],
        procedureName: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ],
        procedureNo: [
          { required: true, message: "流程编码不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "消息标题不能为空", trigger: "blur" }
        ],
        sequenceNo: [
          { required: true, message: "流程ID不能为空", trigger: "blur" }
        ],
        pcUrl: [
          { required: true, message: "待办待阅URL不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
     oneDateRange:{
      handler(newName, oldName) {
        if(newName.length!=0){
          debugger;
          this.twoDateRange = [];
        }
      }
    },
    twoDateRange:{
      handler(newName, oldName) {
        if(newName.length!=0){
          debugger;
          this.oneDateRange = [];
        }
      }
    },
  },
  methods: {
    /** 查询待办待阅信息列表 */
    getList() {
      this.loading = true;
      debugger
      let one = this.oneDateRange;
      let two = this.twoDateRange;
      let newDateRange = [];
      if(one.length!=0){
        newDateRange = one;
        this.queryParams.dateType = "2";;
      }
      if(two.length!=0){
        newDateRange = two;
        this.oneDateRange = [];
        this.queryParams.dateType = "1";
      }
      listPend(this.addDateRange(this.queryParams, newDateRange)).then(response => {
        this.pendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      let queryOa = {"type":4};
      listLog(queryOa).then((response) => {
        if(response.code==200){
          if(response.rows && response.rows[0] && response.rows[0].flag==2){
            this.syncOaMessage = response.rows[0].info;
            return;
          }
        }
        this.syncOaMessage = "OA初始化进行中，请等待！";
      });
      let queryHr = {"type":3};
      listLog(queryHr).then((response) => {
        if(response.code==200){
          if(response.rows && response.rows[0] && response.rows[0].flag==2){
            this.syncHrMessage = response.rows[0].info;
            return;
          }
        }
        this.syncHrMessage = "HR初始化进行中，请等待！";
      });
      let queryBpm = {"type":2};
      listLog(queryBpm).then((response) => {
        if(response.code==200){
          if(response.rows && response.rows[0] && response.rows[0].flag==2){
            this.syncBpmMessage = response.rows[0].info;
            return;
          }
        }
        this.syncBpmMessage = "Bpm初始化进行中，请等待！";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        systemName: null,
        pendType: null,
        promoterNo: null,
        promoter: null,
        promoterOrg: null,
        node: null,
        receiverUserCode: null,
        procedureName: null,
        procedureNo: null,
        title: null,
        sequenceNo: null,
        initiationTime: null,
        receiverTime: null,
        pcUrl: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待办待阅信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPend(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改待办待阅信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePend(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPend(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除待办待阅信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPend(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有待办待阅信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPend(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    pendTypeFormat(pendType){
      if (pendType == '1'){
          return "待办";
      }else{
          return "待阅";
      }
    },
    pendStatusFormat(pendStatus){
      if (pendStatus == '1'){
          return "处理完成";
      }else if(pendStatus == '2'){
          return "未处理";
      }else if(pendStatus == '3'){
        return "已撤回";
      }else{
        return "未知状态";
      }
    },
    syncWorkItemHr() {
      this.$confirm('是否确认同步HR待办待阅信息数据项?', "警告，请先DI操作后，再进行同步", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return syncWorkItemHr();
        }).then(() => {
          this.getList();
          this.msgSuccess("同步成功");
        })
    },

    syncWorkItemOa() {
      this.$confirm('是否确认同步OA待办待阅信息数据项?', "警告，请先DI操作后，再进行同步", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return syncWorkItemOa();
        }).then(() => {
          this.getList();
          this.msgSuccess("同步成功");
        })
    },
    syncWorkItemBpm() {
      this.$confirm('是否确认同步BPM待办待阅信息数据项?', "警告，请先DI操作后，再进行同步", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return syncWorkItemBpm();
        }).then(() => {
          this.getList();
          this.msgSuccess("同步成功");
        })
    },

  }
};
</script>
